import type { User } from '~/types/user';
import { SortBy } from './../types/enums';
import type { UseFetchOptions } from "nuxt/app";

export function useApi<T>(url = "", options: UseFetchOptions<T> = {}) {

  const getUsers = (filterQuery: Ref<string>, sortBy: Ref<SortBy>, asc: Ref<boolean>, page: Ref<number>, perPage:  Ref<number>): Promise<any> => {
      return useFetch(`/api/users`, {
      query: { filterQuery, sortBy, asc, page, perPage },
      lazy: true
    });
  };


  const getUser = (id:  string): Promise<any> => {
      return useFetch(`/api/users/${id}`, {
    });
  };

  const createUser = (user: User | undefined ): Promise<any> => {
      return $fetch(`/api/users/create`,  {
        method: 'POST',
        body: user
    });
  };

  const updateUser = (user: User | undefined): Promise<any> => {
      return $fetch(`/api/users/update`, {
        method: 'PUT',
        body: user
    });
  };



  return {
    getUsers,
    getUser,
    createUser,
    updateUser
  };
}
